package mapkit.layers
{
	import flash.display.DisplayObject;

	import mapkit.MapEvent;
	import mapkit.MapLayer;

	public class GraphicLayer extends MapLayer
	{
		public function addGraphicAt(g:MapGraphic, id:int):void
		{
			if (id < 0 || id > numChildren)
			{
				id=numChildren;
			}

			if (!contains(g as DisplayObject) && map)
			{
				g.map=map;
				g.locator=map.locator;
				addChildAt(g as DisplayObject, id);
			}
			g.invalidateDisplayList();
		}

		public function addGraphic(g:MapGraphic):void
		{
			addGraphicAt(g, numChildren);
		}

		public function removeGraphic(g:MapGraphic):void
		{
			if (contains(g as DisplayObject))
			{
				removeChild(g as DisplayObject);
			}
		}

		override protected function updateMap(event:MapEvent=null):void
		{
			super.updateMap(event);

			invalidateDisplayList();
		}

		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			var g:DisplayObject=null;
			for (var i:int=0; i < numChildren; i++)
			{
				g=getChildAt(i);
				if (g is MapGraphic)
				{
					(g as MapGraphic).invalidateDisplayList();
				}
			}
		}

	}
}